﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Security.Cryptography;
using System.Text;

namespace ManageBanking.Models
{
    public class LoginModels
    {
        public OnlineBankingDataContext olbk = new OnlineBankingDataContext();

        public  string DecryptString(string Message, string Passphrase)
        {
            byte[] Results;
            System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding();
            // Step 1. Bam chuoi su dung MD5
            MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider();
            byte[] TDESKey = HashProvider.ComputeHash(UTF8.GetBytes(Passphrase));
            // Step 2. Tao doi tuong TripleDESCryptoServiceProvider moi
            TripleDESCryptoServiceProvider TDESAlgorithm = new TripleDESCryptoServiceProvider();
            // Step 3. Cai dat bo giai ma
            TDESAlgorithm.Key = TDESKey;
            TDESAlgorithm.Mode = CipherMode.ECB;
            TDESAlgorithm.Padding = PaddingMode.PKCS7;
            // Step 4. Convert chuoi (Message) thanh dang byte[]
            byte[] DataToDecrypt = Convert.FromBase64String(Message);
            // Step 5. Bat dau giai ma chuoi
            try
            {
                ICryptoTransform Decryptor = TDESAlgorithm.CreateDecryptor();
                Results = Decryptor.TransformFinalBlock(DataToDecrypt, 0, DataToDecrypt.Length);
            }
            finally
            {
                // Xoa moi thong tin ve Triple DES va HashProvider de dam bao an toan
                TDESAlgorithm.Clear();
                HashProvider.Clear();
            }
            // Step 6. Tra ve ket qua bang dinh dang UTF8
            return UTF8.GetString(Results);
        }

        public bool Login_Bank(string username, string password)
        {
            Username ac = olbk.Usernames.Where(a => a.Username1 == username.Trim()).First();

            string pass = DecryptString(ac.Password, ac.SecondaryPass);
            int kq = olbk.Usernames.Where(a => a.Username1 == username && password.Trim() == pass.Trim()).Count();
            if (kq == 1)
            {
                return true;
            }
            else
                return false;
        }
        public bool Login_Bank2(string username)
        {
            int b = olbk.Usernames.Where(a => a.Username1 == username && a.IdTypeUser != 1).Count();
            if (b == 0)
                return false;
            else
                return true;
           
        }
        
        public Username Login_Acc(string username)
        {
            Username ac = olbk.Usernames.Where(a => a.Username1 == username).First();
            return ac;
        }
        public void updatelasttime(string username)
        {
            Username ac = olbk.Usernames.Where(a => a.Username1 == username).First();
            ac.LastLogin = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            olbk.SubmitChanges();
        
        }
        public void AddLock(string username)
        {
            Username ac = olbk.Usernames.Where(a => a.Username1 == username).First();
            ac.Lock = true;
            olbk.SubmitChanges();
        
        
        }
        public string getMD5(String str)
        {
            MD5CryptoServiceProvider mdcsp = new MD5CryptoServiceProvider();
            byte[] b = System.Text.Encoding.UTF8.GetBytes(str);
            b = mdcsp.ComputeHash(b);
            StringBuilder s = new StringBuilder();
            foreach (byte by in b)
            {
                s.Append(by.ToString("x2").ToLower());
            }
            return s.ToString();
        }

        /**************************** News  ***********************/

        public List<NewsBanking> ListNews()
        {
            return olbk.NewsBankings.Skip(0).Take(6).ToList();
        }

        /**************************** And News  ***********************/
    }
}
